﻿/*使用说明：
 * 影片剪辑
 * this._scrollable = new MouseScrollableSprite(mc, new Rectangle(0, 0, stage.stageWidth, stage.stageHeight));
addChild(this._scrollable);
 * 转代为位图
 * var _local2:Bitmap = (LoaderInfo(_arg1.target).content as Bitmap);
   this._scrollable = new MouseScrollableSprite(_local2, new Rectangle(0, 0, stage.stageWidth, stage.stageHeight));
   addChild(this._scrollable);
 * 
*/

package org.nickro.externallibrary.display {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import org.nickro.externallibrary.display.*;

    public class MouseScrollableSprite extends PSLCoreSprite {

        public static const HORIZONTAL:String = "horizontal";
        public static const VERTICAL:String = "vertical";
        public static const BOTH:String = "both";
        public static const TL:String = "topLeft";
        public static const CENTER:String = "center";

        private var _scrollTarget:DisplayObject;
        private var _boundRect:Rectangle;
        private var _mouseXPos:Number;
        private var _mouseYPos:Number;
        private var _targetX:Number;
        private var _targetY:Number;
        private var _xDiff:Number;
        private var _yDiff:Number;
        private var _xSensi:Number = 0.2;
        private var _ySensi:Number = 0.2;
        private var _orientation:String;
        private var _startAlign:String;

        public function MouseScrollableSprite(_arg1:DisplayObject, _arg2:Rectangle, _arg3:String="both", _arg4:String="topLeft"){
            this._scrollTarget = _arg1;
            this.boundRect = _arg2;
            this._orientation = _arg3;
            this._startAlign = _arg4;
            this.isEnabled = true;
            if (_arg1 == null){
                return;
            };
            addChild(this._scrollTarget);
            this._positionAtStart();
        }
        private function _positionAtStart():void{
            switch (this._startAlign){
                case TL:
                    this._scrollTarget.x = this._boundRect.x;
                    this._scrollTarget.y = this._boundRect.y;
                    break;
                case CENTER:
                    this._scrollTarget.x = (this._boundRect.x - ((this._scrollTarget.width - this._boundRect.width) * 0.5));
                    this._scrollTarget.y = (this._boundRect.y - ((this._scrollTarget.height - this._boundRect.height) * 0.5));
                    break;
            };
        }
        override public function onAddedToStage(_arg1:Event):void{
            if (isEnabled){
            };
        }
        protected function init():void{
            addEventListener(MouseEvent.MOUSE_MOVE, this._onMouseMove);
            addEventListener(Event.ENTER_FRAME, this._updatePosition);
        }
        override public function onRemovedFromStage(_arg1:Event):void{
            removeEventListener(MouseEvent.MOUSE_MOVE, this._onMouseMove);
            removeEventListener(Event.ENTER_FRAME, this._updatePosition);
        }
        override public function set isEnabled(_arg1:Boolean):void{
            if (((isEnabled) && (_arg1))){
                return;
            };
            super.isEnabled = _arg1;
            if (isEnabled){
                this.init();
            } else {
                removeEventListener(MouseEvent.MOUSE_MOVE, this._onMouseMove);
                removeEventListener(Event.ENTER_FRAME, this._updatePosition);
            };
        }
        private function _onMouseMove(_arg1:MouseEvent):void{
            this._mouseXPos = this._getMouseX();
            this._mouseYPos = this._getMouseY();
            this._targetX = ((100 * (this._mouseXPos + this._boundRect.width)) / this._boundRect.width);
            this._targetY = ((100 * (this._mouseYPos + this._boundRect.height)) / this._boundRect.height);
        }
        private function _getMouseX():Number{
            if (mouseX > (this._boundRect.x + this._boundRect.width)){
                return (this._boundRect.width);
            };
            if (mouseX < this._boundRect.x){
                return (0);
            };
            return ((mouseX - this._boundRect.x));
        }
        private function _getMouseY():Number{
            if (mouseY > (this._boundRect.y + this._boundRect.height)){
                return (this._boundRect.height);
            };
            if (mouseY < this._boundRect.y){
                return (0);
            };
            return ((mouseY - this._boundRect.y));
        }
        private function _updatePosition(_arg1:Event):void{
            this._xDiff = (this._scrollTarget.width - this._boundRect.width);
            this._yDiff = (this._scrollTarget.height - this._boundRect.height);
            var _local2:Number = (((((this._xDiff / 100) * -(this._targetX)) + this._scrollTarget.width) - this._boundRect.width) + this._boundRect.x);
            var _local3:Number = (((((this._yDiff / 100) * -(this._targetY)) + this._scrollTarget.height) - this._boundRect.height) + this._boundRect.y);
            var _local4:Number = ((_local2 - this._scrollTarget.x) * this._xSensi);
            var _local5:Number = ((_local3 - this._scrollTarget.y) * this._ySensi);
            if (((isNaN(_local4)) || (isNaN(_local5)))){
                return;
            };
            switch (this._orientation){
                case HORIZONTAL:
                    this._scrollTarget.x = (this._scrollTarget.x + _local4);
                    break;
                case VERTICAL:
                    this._scrollTarget.y = (this._scrollTarget.y + _local5);
                    break;
                default:
                    this._scrollTarget.x = (this._scrollTarget.x + _local4);
                    this._scrollTarget.y = (this._scrollTarget.y + _local5);
            };
        }
        public function set boundRect(_arg1:Rectangle):void{
            this._boundRect = _arg1;
        }
        public function drawBoundRect(_arg1:Number=0, _arg2:Number=0):void{
            var _local3:Shape = new Shape();
            DisplayUtils.drawRect(_local3.graphics, this._boundRect.x, this._boundRect.y, this._boundRect.width, this._boundRect.height, _arg2, _arg1);
            addChild(_local3);
        }
        public function get scrollTarget():DisplayObject{
            return (this._scrollTarget);
        }
        public function set scrollTarget(_arg1:DisplayObject):void{
            this._scrollTarget = _arg1;
            addChild(this._scrollTarget);
            this._positionAtStart();
        }
        override public function destroy():void{
            super.destroy();
            removeEventListener(MouseEvent.MOUSE_MOVE, this._onMouseMove);
            removeEventListener(Event.ENTER_FRAME, this._updatePosition);
        }
        public function set xSensi(_arg1:Number):void{
            this._xSensi = _arg1;
        }
        public function set ySensi(_arg1:Number):void{
            this._ySensi = _arg1;
        }

    }
}//package org.nickro.externallibrary.display 
